package com.ruoyi.security.service.backtest.gridtrading.strategy.impl;

import com.ruoyi.security.enums.SecurityTradeTypeEnum;
import com.ruoyi.security.service.backtest.gridtrading.strategy.RiseFallTypeCalcStrategy;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 按价格差值计算倍数策略
 * 
 * @author yeoman
 * @date 2025/2/4
 */
@Component("priceDifferenceMultipleStrategy")
public class PriceDifferenceMultipleStrategy implements RiseFallTypeCalcStrategy {
    
    @Override
    public int calculateMultiple(BigDecimal diff, BigDecimal benchmarkPrice, BigDecimal riseFallValue, String tradeType) {
        // 买卖倍数 = 价格差值 / 跌值
        return diff.divide(riseFallValue, 2, RoundingMode.HALF_UP).intValue();
    }

    @Override
    public BigDecimal calculateTriggerPrice(BigDecimal benchmarkPrice, int multiple, BigDecimal riseFallValue, String tradeType) {
        // 总的涨跌值 = 涨跌值 * 倍数
        BigDecimal totalRiseFallValue = riseFallValue.multiply(new BigDecimal(multiple));
        // 触发价 = 基准价 + 总的涨跌值（卖出）
        if (SecurityTradeTypeEnum.SELL.getKey().equals(tradeType)) {
            return benchmarkPrice.add(totalRiseFallValue).setScale(2, RoundingMode.HALF_UP);
        }
        // 触发价 = 基准价 - 总的涨跌值（买入）
        return benchmarkPrice.subtract(totalRiseFallValue).setScale(2, RoundingMode.HALF_UP);
    }

}

